local _, T = ...

local ORL, ORI, AB, collection, collectionID
local seedIDs, seedColors = {79102, 80590, 80591, 80592, 80593, 80594, 80595, 85215, 85216, 85217, 85267, 85268, 85269, 89197, 89202, 89233, 89326, 89328, 89329, 85219, 80302}, "5d9634e9bb78ffcceeffa131c51010dc40b0f9ec0000ce9cff95393971529cff529cff529cff521838087b38088b6d2018a5e64abe104acebd68e62eafff40"
local seedQuests = {[31942]=89329, [31672]=80592, [31673]=80593, [31941]=89328, [31670]=80590, [31669]=79102, [31674]=80594, [31675]=80595, [31943]=89326, [31671]=80591, [31946]=80590, [31947]=79102, [30527]=89329, [31949]=89326, [30254]=80302, [31945]=80591}

-- Check if there are active, uncompleted plant-a-seed quests; return required seed IDs
local function collapse(i, ...)
	CollapseQuestHeader(i)
	return ...
end
local function scanQuestSeeds(i)
	for i=i or 1,GetNumQuestLogEntries() do
		local _, _, _, _, isHeader, isCollapsed, isComplete, _, qid = GetQuestLogTitle(i)
		if isHeader and isCollapsed then
			ExpandQuestHeader(i)
			return collapse(i, scanQuestSeeds(i+1))
		elseif seedQuests[qid] and not select(3, GetQuestLogLeaderBoard(qid == 30254 and 2 or 1, i)) then
			return seedQuests[qid], scanQuestSeeds(i+1)
		end
	end
end

-- Rescan bags whenever ActionBook tells us the collection is about to be displayed
local function syncCollection(ownId, _, openId)
	if ownId ~= openId then return end
	
	local n, changed, questSeed, questSeed2 = 1, nil, scanQuestSeeds()
	for i=1,#seedIDs do local iid = seedIDs[i]
		local action, token = AB:get("item", iid), "Spade.Seed" .. iid
		if action then
			collection[n], collection[token], n, changed = token, action, n + 1, changed or (collection[n] ~= token)
			ORI:SetQuestHint(token, questSeed == iid or questSeed2 == iid)
		else
			collection[token], changed = nil, changed or (collection[token] ~= nil)
		end
	end
	
	if changed then
		for i=n, #collection do
			collection[i] = nil
		end
		AB:update(collectionID, collection)
	end
	
	-- If user hasn't told OPie otherwise, display this ring centered on the mouse, not screen
	if select(3, ORL:GetOption("RingAtMouse", "Spade.Ring")) == nil then
		ORL:SetOption("RingAtMouse", true, "Spade.Ring")
	end
end

function T.InitRing()
	ORL = OneRingLib
	ORI = ORL and ORL.ext and ORL.ext.OPieUI
	AB = ORL and ORL.ext and ORL.ext.ActionBook and ORL.ext.ActionBook:compatible(1, 6)
	if not (ORL and ORI and AB) then return false end
	
	collection = {__openAction=AB:get("macrotext", "/target mouseover")}
	collectionID = AB:create("collection", nil, collection)
	AB:observe("internal.collection.preopen", syncCollection, collectionID)
	ORL:SetRing("Spade.Ring", {name=T.L"Spade", action=collectionID, internal=true, noOpportunisticCA=true, noPersistentCA=true})
	for i=1, #seedIDs*6, 6 do
		ORI:SetDisplayOptions("Spade.Seed" .. seedIDs[(i+5)/6], nil, nil, tonumber(seedColors:sub(i,i+1),16)/255, tonumber(seedColors:sub(i+2,i+3),16)/255, tonumber(seedColors:sub(i+4,i+5),16)/255)
	end
end
function T.BindRing()
	if ORL and ORL.OverrideRingBinding then
		ORL:OverrideRingBinding("Spade.Ring", "BUTTON2")
		return true
	end
end
function T.UnbindRing()
	if ORL and ORL.OverrideRingBinding then
		ORL:OverrideRingBinding("Spade.Ring", nil)
	end
end